﻿

using DualPlaneFR4Paster.Models;
using Newtonsoft.Json;
using NLog;
using Prism.Commands;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;

namespace DualPlaneFR4Paster.ViewModels.Dialogs
{
    public class FeederDialogViewModel : DialogViewModel
    {
        #region 变量
        private static Logger logger = LogManager.GetCurrentClassLogger();
        string partNum = string.Empty;
        #endregion
        private ObservableCollection<FeederDisp> topFloorFeeders;
        public ObservableCollection<FeederDisp> TopFloorFeeders
        {
            get { return topFloorFeeders; }
            set { SetProperty(ref topFloorFeeders, value); }
        }
        private ObservableCollection<FeederDisp> bottomFloorFeeders;
        public ObservableCollection<FeederDisp> BottomFloorFeeders
        {
            get { return bottomFloorFeeders; }
            set { SetProperty(ref bottomFloorFeeders, value); }
        }
        private ObservableCollection<SuckerDisp> topFloorSuckers;
        public ObservableCollection<SuckerDisp> TopFloorSuckers
        {
            get { return topFloorSuckers; }
            set { SetProperty(ref topFloorSuckers, value); }
        }
        private ObservableCollection<SuckerDisp> bottomFloorSuckers;
        public ObservableCollection<SuckerDisp> BottomFloorSuckers
        {
            get { return bottomFloorSuckers; }
            set { SetProperty(ref bottomFloorSuckers, value); }
        }
        private DelegateCommand<object> topFloorPropertyChangedCommand;
        public DelegateCommand<object> TopFloorPropertyChangedCommand =>
            topFloorPropertyChangedCommand ?? (topFloorPropertyChangedCommand = new DelegateCommand<object>(ExecuteTopFloorPropertyChangedCommand));
        private DelegateCommand<object> bottomFloorPropertyChangedCommand;
        public DelegateCommand<object> BottomFloorPropertyChangedCommand =>
            bottomFloorPropertyChangedCommand ?? (bottomFloorPropertyChangedCommand = new DelegateCommand<object>(ExecuteBottomFloorPropertyChangedCommand));
        private DelegateCommand<object> topFloorSuckerPropertyChangedCommand;
        public DelegateCommand<object> TopFloorSuckerPropertyChangedCommand =>
            topFloorSuckerPropertyChangedCommand ?? (topFloorSuckerPropertyChangedCommand = new DelegateCommand<object>(ExecuteTopFloorSuckerPropertyChangedCommand));
        private DelegateCommand<object> bottomFloorSuckerPropertyChangedCommand;
        public DelegateCommand<object> BottomFloorSuckerPropertyChangedCommand =>
            bottomFloorSuckerPropertyChangedCommand ?? (bottomFloorSuckerPropertyChangedCommand = new DelegateCommand<object>(ExecuteBottomFloorSuckerPropertyChangedCommand));

        void ExecuteBottomFloorSuckerPropertyChangedCommand(object obj)
        {
            string name = (string)obj;
            using (var db = new DTContext(partNum))
            {
                var mparam = db.MParams.FirstOrDefault(p => p.Name == name && p.Floor == 2);
                if (mparam != null)
                {
                    var sucker = BottomFloorSuckers.FirstOrDefault(p => p.Name == name);
                    if (sucker != null)
                    {
                        Sucker f1 = new Sucker
                        {
                            FeederIndex = sucker.FeederIndex
                        };
                        string newvalue = JsonConvert.SerializeObject(f1, Formatting.Indented);
                        logger.Info($"{MLogType.调试}:{(mparam.Floor == 1 ? "上层" : "下层")}卷料\"{mparam.Name}\"({mparam.Value})→({newvalue})");
                        mparam.Value = newvalue;
                        db.SaveChanges();
                    }
                }
            }
        }
        void ExecuteTopFloorSuckerPropertyChangedCommand(object obj)
        {
            string name = (string)obj;
            using (var db = new DTContext(partNum))
            {
                var mparam = db.MParams.FirstOrDefault(p => p.Name == name && p.Floor == 1);
                if (mparam != null)
                {
                    var sucker = TopFloorSuckers.FirstOrDefault(p => p.Name == name);
                    if (sucker != null)
                    {
                        Sucker f1 = new Sucker
                        {
                            FeederIndex = sucker.FeederIndex
                        };
                        string newvalue = JsonConvert.SerializeObject(f1, Formatting.Indented);
                        logger.Info($"{MLogType.调试}:{(mparam.Floor == 1 ? "上层" : "下层")}卷料\"{mparam.Name}\"({mparam.Value})→({newvalue})");
                        mparam.Value = newvalue;
                        db.SaveChanges();
                    }
                }
            }
        }
        void ExecuteBottomFloorPropertyChangedCommand(object obj)
        {
            string name = (string)obj;
            using (var db = new DTContext(partNum))
            {
                var mparam = db.MParams.FirstOrDefault(p => p.Name == name && p.Floor == 2);
                if (mparam != null)
                {
                    var feeder = BottomFloorFeeders.FirstOrDefault(p => p.Name == name);
                    if (feeder != null)
                    {
                        Feeder f1 = new Feeder
                        {
                            ColumnCount = feeder.ColumnCount,
                            deltaX = feeder.deltaX,
                            deltaY = feeder.deltaY,
                            IsUsed = feeder.IsUsed
                        };
                        string newvalue = JsonConvert.SerializeObject(f1, Formatting.Indented);
                        logger.Info($"{MLogType.调试}:{(mparam.Floor == 1 ? "上层" : "下层")}卷料\"{mparam.Name}\"({mparam.Value})→({newvalue})");
                        mparam.Value = newvalue;
                        db.SaveChanges();
                    }
                }
            }
        }
        void ExecuteTopFloorPropertyChangedCommand(object obj)
        {
            string name = (string)obj;
            using (var db = new DTContext(partNum))
            {
                var mparam = db.MParams.FirstOrDefault(p => p.Name == name && p.Floor == 1);
                if (mparam != null)
                {
                    var feeder = TopFloorFeeders.FirstOrDefault(p => p.Name == name);
                    if (feeder != null)
                    {
                        Feeder f1 = new Feeder
                        {
                            ColumnCount = feeder.ColumnCount,
                            deltaX = feeder.deltaX,
                            deltaY = feeder.deltaY,
                            IsUsed = feeder.IsUsed
                        };
                        string newvalue = JsonConvert.SerializeObject(f1, Formatting.Indented);
                        logger.Info($"{MLogType.调试}:{(mparam.Floor == 1 ? "上层":"下层")}卷料\"{mparam.Name}\"({mparam.Value})→({newvalue})");
                        mparam.Value = newvalue;
                        db.SaveChanges();
                    }
                }
            }
        }
        public override void OnDialogOpened(IDialogParameters parameters)
        {
            partNum = parameters.GetValue<string>("PartNum");
            using (var db = new DTContext(partNum))
            {
                var mparams = db.MParams.Where(p => p.Name.Contains("Feeder") && p.Floor == 1);
                if (mparams.Any())
                {
                    var mparamList = mparams.ToList();
                    foreach (var item in mparamList)
                    {
                        var feeder = JsonConvert.DeserializeObject<Feeder>(item.Value);
                        TopFloorFeeders.Add(new FeederDisp
                        {
                            Name = item.Name,
                            Alias = item.Alias,
                            IsUsed = feeder.IsUsed,
                            ColumnCount = feeder.ColumnCount,
                            deltaX = feeder.deltaX,
                            deltaY = feeder.deltaY
                        });
                    }
                }

                mparams = db.MParams.Where(p => p.Name.Contains("Feeder") && p.Floor == 2);
                if (mparams.Any())
                {
                    var mparamList = mparams.ToList();
                    foreach (var item in mparamList)
                    {
                        var feeder = JsonConvert.DeserializeObject<Feeder>(item.Value);
                        BottomFloorFeeders.Add(new FeederDisp
                        {
                            Name = item.Name,
                            IsUsed = feeder.IsUsed,
                            Alias = item.Alias,
                            ColumnCount = feeder.ColumnCount,
                            deltaX = feeder.deltaX,
                            deltaY = feeder.deltaY
                        });
                    }
                }

                mparams = db.MParams.Where(p => p.Name.Contains("Sucker") && p.Floor == 1);
                if (mparams.Any())
                {
                    var mparamList = mparams.ToList();
                    foreach (var item in mparamList)
                    {
                        var sucker = JsonConvert.DeserializeObject<Sucker>(item.Value);
                        TopFloorSuckers.Add(new SuckerDisp
                        {
                            Name = item.Name,
                            Alias = item.Alias,
                            FeederIndex = sucker.FeederIndex
                        });
                    }
                }
                mparams = db.MParams.Where(p => p.Name.Contains("Sucker") && p.Floor == 2);
                if (mparams.Any())
                {
                    var mparamList = mparams.ToList();
                    foreach (var item in mparamList)
                    {
                        var sucker = JsonConvert.DeserializeObject<Sucker>(item.Value);
                        BottomFloorSuckers.Add(new SuckerDisp
                        {
                            Name = item.Name,
                            Alias = item.Alias,
                            FeederIndex = sucker.FeederIndex
                        });
                    }
                }
            }
        }
        public FeederDialogViewModel(IContainerProvider containerProvider) : base(containerProvider)
        {
            Title = "卷料和吸头";
            TopFloorFeeders = new ObservableCollection<FeederDisp>();
            BottomFloorFeeders = new ObservableCollection<FeederDisp>();
            TopFloorSuckers = new ObservableCollection<SuckerDisp>();
            BottomFloorSuckers = new ObservableCollection<SuckerDisp>();
        }
    }
}
